package cn.itcast.controller;

import cn.itcast.domain.Book;
import cn.itcast.domain.Kind;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/kind")
//往session中存数据;
@SessionAttributes("map")
public class KindController {

    @RequestMapping("/ kind1")
    public void kind1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("请求转发或者重定向");
        req.getRequestDispatcher("../view/book/success.jsp").forward(req,resp);
        resp.sendRedirect("../view/book/success.jsp");

    }


    @RequestMapping("/kind2")
    public void kind2() {

    }

    @RequestMapping("/ kind3")
    public String kind3() {
        System.out.println("kind3");
        return "redirect:../view/book/success.jsp";
    }

    @RequestMapping("/kind4")
    @ResponseBody
    //String
    public Book kind4(Kind kind) {
        System.out.println("异步请求方法执行了");
        System.out.println(kind);
        Book book = new Book();
        book.setBookName("1111111");
        book.setPrice(200d);
        System.out.println(book);
        return book;

    }

    @RequestMapping("/ kind5")
    //Model model往request域中存数据；
    public String kind5(Model model){
        System.out.println("存进来了");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("key","value");
        map.put("name","zhansan");
        map.put("age","20");
        model.addAttribute("map",map);
        return  "book/success";
    }

    @RequestMapping("/ kind6")
    public String kind6(@SessionAttribute Map<String, Object> map) {
        System.out.println("删除了");
        //移除一个数据;
         map.remove("age");
        return "book/success";
    }
}
